<?
require( "Application.php"); 

if(empty($_GET["tester"]) && $_GET["tester"] != "Y") {
	die("Hey you");
}
$storeManager = new StoreManager();
$yelpManager = new YelpManager();
$hasStore=true;
$stores = $storeManager->getStoresForYelp();
echo "size of stores: " . count($stores);
for($i=0;$i<count($stores);$i++)
{
	$store = $stores[$i];
	echo "Process Store: " . $store->name . " Search term " . $store->yelpTerm . "<br>";
	$hasStore=true;
	try{
	  $yelpManager->searchTerm($store->storeId, $store->yelpTerm, $store->yelpCity);
	}catch(Exception $e){
	  $hasStore=false;
	}
	
	if($hasStore && $yelpManager->isYelpRequest()){
		echo "legit Yelp<br>";
	  //var_dump($yelpManager->getStores());
	  $yelpStores = $yelpManager->getStores();
	  echo "Is array: "  . is_array($yelpStores) . "<br>";
	  if (is_array($yelpStores)){
	  	echo "Associated stores: " . count($yelpStores) . "<br>";
	    for($x=0;$x<count($yelpStores);$x++){
	    	echo "Loop through yelpStores<br>";
	      $yelpStore = $yelpStores[$x];
	      echo $yelpStore->name . "<br>";
	      if(is_array($yelpStore->reviews) && count($yelpStore->reviews) > 0){
	        $yelpManager->saveStore($yelpStore);
	      }
	    }
	  }
	}else{
	  echo "<p>Invalid Yelp request</p>";
	}
}





require( "OnRequestEnd.php"); 

?>